// pages/find/userPage/userPage.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0, //tab激活下标
    showHome:false,  //false  不展示回到首页，展示回到上一页
    list:[],
    showGuanzhu: true,
    pageNum: 1,
    pageSize: 3, 
    userList:[],
    userInfo:[],
    num:0,
    form:{
      mobileUserId:''
    }
  },
  // tab激活下标
  onChangetab(event) {
    console.log(event.detail.name)
    this.setData({
      active: event.detail.name,
      list:[],
      pageNum:1,
    })
    this.onShow()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (app.globalData.hasLogin == true) {
      // this.checkcommentListZan()
      this.followList()
      // this.collectList()
    }
    console.log(options)
    this.setData({
      'form.mobileUserId':options.mobileUserId
    })
    let that = this
    var pages = getCurrentPages() //获取加载的页面
    console.log(pages);
    if (pages.length == 1) {
      that.setData({
        showHome: true
      })
    } else {
      that.setData({
        showHome: false
      })
    }
  },
//详情
  tomain:function(e){
    if(this.data.active == 0){
         const dynamicId = e.currentTarget.dataset.dynamicid //跳转参数
          // const path = e.currentTarget.dataset.path
      
          wx.navigateTo({
            url: `/pages/find/dynamicDetail/dynamicDetail?dynamicId=${dynamicId}`
          })

    } else{
        const momentId = e.currentTarget.dataset.momentid //跳转参数
    const path = e.currentTarget.dataset.path
    // console.log(path + "?momentId=" + momentId);

    wx.navigateTo({
      url: `/pages/articleDetails/articleDetails?momentId=${momentId}`
    })
    }
  },

    //获取关注列表
    followList() {
      var postObj = {
        url: `/mobileUser/follow/list`,
        method: 'get'
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          var userId = this.data.form.mobileUserId
          console.log('获取关注列表', res)
          var followList = res.data.data
          for (let index = 0; index < followList.length; index++) {
            const element = followList[index];
            if (element.userId == userId) {
              this.setData({
                showGuanzhu: false
              })
              break;
            }
          }
          this.setData({
            followList: followList
          })
        }
      })
    },


// 新增关注 POST/mobileUser/follow  
  followYes() {
    var userId = this.data.form.mobileUserId
    if (app.checkHasLogin()) {
      var postObj = {
        url: `/mobileUser/follow`,
        method: 'post',
        data: {
          userId: userId, //被关注人的id
        }
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          console.log('新增关注', res)
          wx.showToast({
            title: '关注成功',
            image: '../../../assets/success.png',
            duration: 1000,
          })
          this.setData({
            showGuanzhu: false
          })
          this.followList()
        }
      })
    }

  },
  // 删除关注 /mobileUser/follow/edit删除关注
  followNo() {
    var userId = this.data.form.mobileUserId
    if (app.checkHasLogin()) {
      var postObj = {
        url: `/mobileUser/follow/edit`,
        method: 'put',
        data: {
          userId: userId
        }
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          console.log('取消关注成功', res)
          wx.showToast({
            title: '取消关注',
            image: '../../../assets/success.png',
            duration: 1000,
          })
          this.setData({
            showGuanzhu: true
          })
          this.followList()
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
     


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    let that = this
    if(this.data.form.mobileUserId){
      that.setData({
        num:1
      })
      that.getUser()
    } else {
      var userform  =  app.globalData.userInfo
      this.setData({userInfo:userform})
      var postObj = {
        url: '/mobileUser/dynamic/getMyInformation',
        method: 'get'
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          res.data.data.mobileUser.image =  app.util.config().api + res.data.data.mobileUser.image
          this.setData({
            userList:res.data.data
          })
        }

      })
    //收藏
    if(that.data.active == 1){
      var postObj = {
        url: '/mobileUser/collection/list?pageNum=' + this.data.pageNum + '&pageSize=' + this.data.pageSize ,
        method: 'get'
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          for(var i=0;i<res.data.rows.length;i++)
              {
                
                var rich = res.data.rows[i].sysMoment.detail
                rich = app.util.formatRichText(rich)
                // console.log(rich)
                // console.log( res.data.rows[i].sysMoment.detail )
                for( var k=0;k<res.data.rows[i].sysMoment.sysMomentFile.length;k++)
                {
                  res.data.rows[i].sysMoment.sysMomentFile[k].url = app.util.config().api + res.data.rows[i].sysMoment.sysMomentFile[k].url
                }
              }
          // that.setData({
          //   list: res.data.rows
          // })
          var navList = this.data.list
          // console.log("123",navList)
          var rows = res.data.rows
          var allArr = [...navList, ...rows]
          this.setData({
            list  : allArr
          })
        }
      })
    } else{
      //动态
    var postObj = {
      url: '/mobileUser/dynamic/getMyList?pageNum=' + this.data.pageNum + '&pageSize=' + this.data.pageSize ,
      method: 'get'
    }
    app.request.wxRequest(postObj).then(res => {
      if (res.data.code === 200) {
        // that.setData({
        //   list: res.data.rows
        // })
        for(var i=0;i<res.data.rows.length;i++)
        {
          for( var k=0;k<res.data.rows[i].dynamicFileList.length;k++)
          {
            res.data.rows[i].dynamicFileList[k].url = app.util.config().api + res.data.rows[i].dynamicFileList[k].url
          }
        }
        var navList = this.data.list
        // console.log("123",navList)
        var rows = res.data.rows
        var allArr = [...navList, ...rows]
        this.setData({
          list  : allArr
        })
      }
    })
  }
}
  },

  getUser:function(){
    var that = this
    var postObj = {
      url: '/mobileUser/dynamic/getPersonInformation',
      method: 'get',
      data:this.data.form
    }
    app.request.wxRequest(postObj).then(res => {
      if (res.data.code === 200) {
        res.data.data.mobileUser.image =  app.util.config().api + res.data.data.mobileUser.image
        this.setData({
          userList:res.data.data
        })
   
      }
    })
    if(that.data.active == 1){
      var postObj = {
        url: '/mobileUser/dynamic/getMomentCollection?pageNum=' + this.data.pageNum + '&pageSize=' + this.data.pageSize ,
        method: 'get',
        data:this.data.form
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          for(var i=0;i<res.data.rows.length;i++)
              {
                var rich = res.data.rows[i].sysMoment.detail
                rich = app.util.formatRichText(rich)
                // console.log(rich)
                console.log( res.data.rows[i].sysMoment.detail )
                for( var k=0;k<res.data.rows[i].sysMoment.sysMomentFile.length;k++)
                {
                  res.data.rows[i].sysMoment.sysMomentFile[k].url = app.util.config().api + res.data.rows[i].sysMoment.sysMomentFile[k].url
                }
              }
          // that.setData({
          //   list: res.data.rows
          // })
          var navList = this.data.list
          // console.log("123",navList)
          var rows = res.data.rows
          var allArr = [...navList, ...rows]
          this.setData({
            list  : allArr
          })
        }
      })
    } else{
      //动态
    var postObj = {
      url: '/mobileUser/dynamic/getOthersDynamicList?pageNum=' + this.data.pageNum + '&pageSize=' + this.data.pageSize ,
      method: 'get',
      data:this.data.form
    }
    app.request.wxRequest(postObj).then(res => {
      if (res.data.code === 200) {
        // that.setData({
        //   list: res.data.rows
        // })
        for(var i=0;i<res.data.rows.length;i++)
        {
          for( var k=0;k<res.data.rows[i].dynamicFileList.length;k++)
          {
            res.data.rows[i].dynamicFileList[k].url = app.util.config().api + res.data.rows[i].dynamicFileList[k].url
          }
        }
        var navList = this.data.list
        // console.log("123",navList)
        var rows = res.data.rows
        var allArr = [...navList, ...rows]
        this.setData({
          list  : allArr
        })
      }
    })
  }
    // GET/mobileUser/dynamic/getPersonInformation
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    that.setData({
      pageNum: that.data.pageNum + 1
    })
    that.onShow()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  toback:function(){
    wx.navigateBack({
      delta: -1,
    })
  },
  toHome: function () {
    wx.reLaunch({
      url: '/pages/allIndex/allIndex',
    })
  }
})